const {nanoid}=require("nanoid")
const {sqlConnect}=require("../model/index")
let express = require("express");
const { json } = require("body-parser");
itineraryRouter=express.Router()
itineraryRouter.get('/admin/itinerary-info/',function(req,res){
    let sql=`select DATE_FORMAT(s_time,'%Y-%m-%d %H:%i') as s_time,fullname,idno,flight_id,seat_no, a.location_name as departure_name,b.location_name as destination_name
    from location as a,location as b,airline_info,person,seat
    where person.id=seat.p_id and seat.a_id=airline_info.id and a.id=airline_info.s_loc_id and b.id=airline_info.e_loc_id
     `
    console.log("itinerary GET请求到了服务器,向数据库更改一条数据")
    sqlConnect.query(sql,function(err,result){
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            return;
          }
         console.log('--------------------------SELECT----------------------------');
         console.log(result);
         console.log('------------------------------------------------------------\n\n');  
         res.json({
          data:result,
          success: true,
        });
    })
})
itineraryRouter.post('/admin/itinerary-info/search-client',function(req,res){
    let sql=`select DATE_FORMAT(s_time,'%Y-%m-%d %H:%i') as s_time,fullname,idno,flight_id,seat_no, a.location_name as departure_name,b.location_name as destination_name
    from location as a,location as b,airline_info,person,seat
    where person.id=seat.p_id and seat.a_id=airline_info.id and a.id=airline_info.s_loc_id and b.id=airline_info.e_loc_id
    and idno=?`
    console.log("itinerary search-client/POST请求到了服务器,向数据库更改一条数据")
    console.log(req.body)
    var {idno,fullname}=req.body
    console.log(idno,fullname)
    sqlConnect.query(sql,idno,function(err,result){
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log('--------------------------SELECT----------------------------');
        console.log(result);
        console.log('------------------------------------------------------------\n\n');  
        res.json({
         data:result,
         success: true,
       })
    })
})
itineraryRouter.post('/admin/itinerary-info/search-airline',function(req,res){
    let sql=`select DATE_FORMAT(s_time,'%Y-%m-%d %H:%i') as s_time,fullname,idno,flight_id,seat_no, a.location_name as departure_name,b.location_name as destination_name
    from location as a,location as b,airline_info,person,seat
    where person.id=seat.p_id and seat.a_id=airline_info.id and a.id=airline_info.s_loc_id and b.id=airline_info.e_loc_id
    and flight_id=`
    console.log("itinerary search-airline/POST请求到了服务器,向数据库更改一条数据")
    console.log(req.body)
    var {flight_id,e_loc_id,s_loc_id,s_time}=req.body
    sql+=`"${flight_id}"`+`and e_loc_id=`
    sql+=`"${e_loc_id}"`+'and s_loc_id='
    sql+=`"${s_loc_id}"`+"and DATE_FORMAT(s_time,'%Y-%m-%d')="+`"${s_time}"`
    sqlConnect.query(sql,function(err,result){
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log('--------------------------SELECT----------------------------');
        console.log(result);
        console.log('------------------------------------------------------------\n\n');  
        res.json({
         data:result,
         success: true,
       })
    })
})
module.exports={
    itineraryRouter
}